cmake_minimum_required(VERSION 3.21.1)

option(LINK_INSIGHT "Link Qt Insight Tracker library" ON)
option(BUILD_QDS_COMPONENTS "Build design studio components" ON)

project(DrillingrigApp LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml)
set(QML_IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY}
    CACHE STRING "Import paths for Qt Creator's code model"
    FORCE
)
if(WIN32)
    set(app_icon_resource_windows "${CMAKE_CURRENT_SOURCE_DIR}/app.rc")
    set(BUILD_SYSTEM WIN32)
endif()
find_package(Qt6 6.2 REQUIRED COMPONENTS Core Gui Qml Quick Widgets)
find_package(Qt6 COMPONENTS ShaderTools)

if (Qt6_VERSION VERSION_GREATER_EQUAL 6.3)
    qt_standard_project_setup()
endif()

qt_add_executable(DrillingrigApp 
    src/main.cpp
    src/ConfigHelper.h
    src/ConfigHelper.cpp
    ${app_icon_resource_windows}
)

qt_add_resources(DrillingrigApp "configuration"
    PREFIX "/"
    FILES
        qtquickcontrols2.conf
)

target_include_directories(DrillingrigApp
	PRIVATE ../DrillingrigCore
)

target_link_libraries(DrillingrigApp PRIVATE
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Quick
    Qt${QT_VERSION_MAJOR}::Qml
    Qt${QT_VERSION_MAJOR}::Widgets
    DrillingrigCore
)

if (BUILD_QDS_COMPONENTS)
    include(${CMAKE_CURRENT_SOURCE_DIR}/qmlcomponents)
endif()

include(${CMAKE_CURRENT_SOURCE_DIR}/qmlmodules)

if (LINK_INSIGHT)
    include(${CMAKE_CURRENT_SOURCE_DIR}/insight)
endif ()

install(TARGETS DrillingrigApp
     BUNDLE  DESTINATION .
     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

set(deploy_script "${CMAKE_CURRENT_BINARY_DIR}/deploy_DrillingrigApp.cmake")
file(GENERATE OUTPUT ${deploy_script} CONTENT "
 include(\"${QT_DEPLOY_SUPPORT}\")
 qt_deploy_qml_imports(
     # Deploy QML modules used by MyApp
     TARGET DrillingrigApp

     # The found QML plugins are stored in the plugins_found variable
     PLUGINS_FOUND plugins_found

     # The QML modules will be deployed into a custom directory
     QML_DIR \"qml\"

     # Qt QML modules will be skipped, only project-created QML modules will be deployed
     # NO_QT_IMPORTS
 )
 # Deploy application runtime dependencies and runtime dependencies
 # of the found QML module plugins.
 qt_deploy_runtime_dependencies(
     EXECUTABLE \$<TARGET_FILE:DrillingrigApp>
     ADDITIONAL_LIBRARIES \$<TARGET_FILE:DrillingrigCore>
     ADDITIONAL_MODULES \${plugins_found}
     GENERATE_QT_CONF
     VERBOSE
 )
 ")
#qt_generate_deploy_qml_app_script(
#    TARGET DrillingrigApp
#    OUTPUT_SCRIPT deploy_script
#    MACOS_BUNDLE_POST_BUILD
#    NO_UNSUPPORTED_PLATFORM_ERROR
#    DEPLOY_USER_QML_MODULES_ON_UNSUPPORTED_PLATFORM
#)
install(SCRIPT ${deploy_script})

set_property(TARGET DrillingrigApp PROPERTY FOLDER "Exec")